package cn.sq.musicserver.scheduling;

import org.quartz.Scheduler;
import org.quartz.spi.JobFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;

/**
 * Created with IntelliJ IDEA.
 * User: SQ
 * Date: 2019/4/11
 * Time: 16:01
 * Description: 定时任务调度类  定时问题配置
 */
@Configuration
public class SchedulerConfig {
    /**
     * 读取quartz.properties 文件
     * 将值初始化
     * @return
     */
//    @Autowired
//    DataSource dataSource;
    @Autowired
    private JobFactory jobFactory;


    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setJobFactory(jobFactory);
        // 用于quartz集群,QuartzScheduler 启动时更新己存在的Job
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        //延长启动
        schedulerFactoryBean.setStartupDelay(1);
        //设置加载的配置文件
//        schedulerFactoryBean.setConfigLocation(new ClassPathResource("/quartz.properties"));
//        schedulerFactoryBean.setDataSource(dataSource);
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler() {
        return schedulerFactoryBean().getScheduler();
    }





}
